define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) 
                {
                    Controller[panel.attr("id")].call(this);

                    $(this).on('click', function (e) 
                    {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }

                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
            Controller.api.bindevent();
        },
        info:function(){
            // 初始化表格参数配置
            var ids = Fast.api.query('ids');
            Table.api.init({
                extend: {
                    index_url: `business/info/index?ids=${ids}`,
                    table: 'business',
                }
            });

            var table = $("#InfoTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search : false,
                toolbar: '#InfoToolbar',
                fixedColumns: true, 
                commonSearch: false,//普通搜索
                cardView: true,     //卡片
                showToggle: false,  //切换表格卡片
                showColumns: false, //可选列
                showExport: false,  //导出
                pagination: false,  //分页
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {
                            field: 'gender', 
                            title: __('Gender'),
                            formatter: Table.api.formatter.flag,
                            custom:{'0': 'info','1':'success', '2':'danger'},
                            searchList: {"0":__('Secrecy'),"1":__('Male'),"2":__('FeMale')},
                        },
                        {field: 'source.name', title: __('Source')},
                        {
                            field: 'deal', 
                            title: __('Deal'),
                            formatter: Table.api.formatter.flag,
                            custom:{'0': 'danger','1':'success'},
                            searchList: {"0":__('Deal 0'),"1":__('Deal 1')},
                        },
                        {field: 'openid', title: __('Openid')},
                        {field: 'province_text', title: __('Province')},
                        {field: 'city_text', title: __('City')},
                        {field: 'district_text', title: __('District')},
                        {field: 'createtime', title: __('Createtime'), addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'money', title: __('Money')},
                        {field: 'email', title: __('Email')},
                        {
                            field: 'auth', 
                            title: __('Auth'),
                            formatter: Table.api.formatter.flag,
                            custom:{'0': 'danger','1':'success'},
                            searchList: {"0":__('Auth 0'),"1":__('Auth 1')},
                        },
                        {field: 'poster', title: __('Poster'),events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'parentid', title: __('Parentid')},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        receive: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `business/info/receive?ids=${ids}`,
                    table: 'business_receive',
                }
            });

            var table = $("#ReceiveTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'receive.applytime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#ReceiveToolbar',
                fixedColumns: true, 
                search: true,
                columns: [
                    [
                        {field: 'applytime', title: __('Applytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'admin.nickname', title: __('Admin'), operate: 'LIKE'},
                        {
                            field: 'status', 
                            title: __('Status'), 
                            formatter: Table.api.formatter.status,
                            custom:{ 'apply': 'success','allot': 'yellow', 'recovery': 'blue', 'reject': 'danger',},
                            searchList: {'apply':__('Apply'),'allot':__('Allot'),'recovery':__('Recovery'),'reject':__('Reject')}, 
                        },
                       
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        visit: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `business/visit/index?ids=${ids}`,
                    add_url: `business/visit/add?ids=${ids}`,
                    edit_url: `business/visit/edit`,
                    del_url: 'business/visit/del',
                    multi_url: 'business/visit/multi',
                    import_url: 'business/visit/import',
                    table: 'business_visit',
                }
            });

            var table = $("#VisitTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'visit.createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#VisitToolbar',
                fixedColumns: true, 
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        {field: 'admin.nickname', title: __('Admin'),operate:'LIKE'},
                        {field: 'content', title: __('Content'),operate:'LIKE',formatter:Table.api.formatter.content, table: table, class: 'autocontent'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            Table.api.bindevent(table);
            $('.btn-visit-add').click(function(){
                Fast.api.open($.fn.bootstrapTable.defaults.extend.add_url, '添加')
            })
        },
        record: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `business/info/record?ids=${ids}`,
                    table: 'business_record',
                }
            });

            var table = $("#RecordTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#RecordToolbar',
                fixedColumns: true, 
                search: false,
                columns: [
                    [
                        {field: 'content', title: __('Content'),operate:'LIKE',formatter:Table.api.formatter.content, table: table, class: 'autocontent'},
                        {field: 'total', title: __('Total(￥)'), operate: '='},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        order: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `business/info/order?ids=${ids}`,
                    table: 'subject_order',
                }
            });

            var table = $("#SubjectTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#SubjectToolbar',
                fixedColumns: true, 
                search: false,
                columns: [
                    [
                        { field: 'code', title: __('SubjectCode'), sortable: false, operate: 'LIKE' },
                        { field: 'subject.title', title: __('SubjectName'), sortable: true, operate: 'LIKE' },
                        {field: 'total', title: __('Total(￥)'), operate: '='},
                        {
                            field: 'pay', 
                            title: __('Pay'), 
                            formatter: Table.api.formatter.status,
                            custom:{ 'money': 'red','zfb': 'blue', 'wx': 'green'},
                            searchList: {'money':__('Money'),'zfb':__('Zfb'),'wx':__('Wx')}, 
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        coupon: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `business/info/coupon?ids=${ids}`,
                    table: 'coupon_receive',
                }
            });

            var table = $("#CouponTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#CouponToolbar',
                fixedColumns: true, 
                search: false,
                columns: [
                    [
                        { field: 'coupon.title', title: __('SubjectName'), sortable: true, operate: 'LIKE' },
                        {
                            field: 'status', 
                            title: __('Status'), 
                            formatter: Table.api.formatter.status,
                            custom:{ '1': 'success','0': 'danger'},
                            searchList: {'1':__('Available'),'0':__('Unavailable')}, 
                        },
                        {field: 'createtime', title: __('ReceiveCreatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'coupon.createtime', title: __('CouponCreatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'coupon.endtime', title: __('CouponEndtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        commission: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `business/info/commission?ids=${ids}`,
                    table: 'business_commission',
                }
            });

            var table = $("#CommissionTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#CommissionToolbar',
                fixedColumns: true, 
                search: false,
                columns: [
                    [
                        { field: 'order.code', title: __('SubjectCode'), sortable: false, operate: 'LIKE' },
                        { field: 'business.nickname', title: __('SubjectName'), sortable: true, operate: 'LIKE' },
                        {field: 'amount', title: __('Amount(￥)'), operate: '='},
                        {
                            field: 'type', 
                            title: __('Type'), 
                            formatter: Table.api.formatter.status,
                            custom:{ 'subject': 'success','product': 'danger'},
                            searchList: {'subject':__('Subject'),'product':__('Product')}, 
                        },
                        {
                            field: 'status', 
                            title: __('Status'), 
                            formatter: Table.api.formatter.status,
                            custom:{ '0': 'danger','1': 'success'},
                            searchList: {'0':__('NotWithdrawn'),'1':__('Withdrawn')}, 
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        address: function(){
            console.log(1)
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
